package com.ruoyi.wzgl.wzxx.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.wzgl.wzxx.entry.*;
import com.ruoyi.wzgl.wzxx.domain.WzHkVo;
import com.ruoyi.wzgl.wzxx.domain.Wzxx;
import com.ruoyi.wzgl.wzxx.service.IWzxxService;
import com.ruoyi.wzgl.wzxx.domain.CkHjFcVo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.util.List;

/**
 * 物资基本信息Controller
 *
 * @author huacai
 * @date 2024-09-19
 */
@RestController
@RequestMapping("/wzgl/wzxx")
public class WzxxController extends BaseController {
    @Resource
    private IWzxxService wzxxService;

    /**
     * 查询物资基本信息列表
     */
//    @PreAuthorize("@ss.hasPermi('wzgl:wzxx:list')")
    @GetMapping("/list")
    public TableDataInfo list(Wzxx wzxx) {
        startPage();
        List<Wzxx> list = wzxxService.selectWzxxList(wzxx);
        return getDataTable(list);
    }

    /**
     * 查询库存小于安全库存的数据
     */
    @GetMapping("/selectAqkcList")
    public TableDataInfo selectAqkcList(Wzxx wzxx) {
        startPage();
        List<Wzxx> list = wzxxService.selectAqkcList(wzxx);
        return getDataTable(list);
    }

    /**
     * 查询物资基本信息列表(所有部门)
     */
    @GetMapping("/selectList")
    public TableDataInfo selectList(Wzxx wzxx) {
        startPage();
        List<Wzxx> list = wzxxService.selectList(wzxx);
        return getDataTable(list);
    }

    /**
     * 查询物资基本信息列表(库存大于0, 用于物资出库明细选择)
     */
    @GetMapping("/kcDyList")
    public TableDataInfo selectKcDyList(Wzxx wzxx) {
        startPage();
        List<Wzxx> list = wzxxService.selectKcDyList(wzxx);
        return getDataTable(list);
    }

    /**
     * 导出物资基本信息列表
     */
    @PreAuthorize("@ss.hasPermi('wzgl:wzxx:export')")
    @Log(title = "物资基本信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Wzxx wzxx) {
        List<Wzxx> list = wzxxService.selectWzxxList(wzxx);
        ExcelUtil<Wzxx> util = new ExcelUtil<Wzxx>(Wzxx.class);
        util.exportExcel(response, list, "物资基本信息数据");
    }

    /**
     * 下载模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<Wzxx> util = new ExcelUtil<Wzxx>(Wzxx.class);
        util.importTemplateExcel(response, "物资基本信息数据");
    }

    /**
     * 导入数据
     */
    @Log(title = "物资基本信息", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception {
        ExcelUtil<Wzxx> util = new ExcelUtil<Wzxx>(Wzxx.class);
        InputStream inputStream = file.getInputStream();
        List<Wzxx> list = util.importExcel(inputStream);
        // 对导入的数据进行处理
        for (Wzxx wzxx : list) {
            wzxx.setDeptId(Math.toIntExact(getDeptId()));
            wzxx.setUserId(Math.toIntExact(getUserId()));
            wzxx.setCreateBy(getUsername());
            wzxx.setCkGlZt("正常关联");
        }
        inputStream.close();
        int count = wzxxService.batchInsertWzxx(list);
        return AjaxResult.success("导入成功" + count + "条信息！");
    }

    /**
     * 获取物资基本信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('wzgl:wzxx:query')")
    @GetMapping(value = "/{wzxxId}")
    public AjaxResult getInfo(@PathVariable("wzxxId") Integer wzxxId) {
        return success(wzxxService.selectWzxxByWzxxId(wzxxId));
    }

    /**
     * 新增物资基本信息
     */
    @PreAuthorize("@ss.hasPermi('wzgl:wzxx:add')")
    @Log(title = "物资基本信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Wzxx wzxx) {
        wzxx.setUserId(Math.toIntExact(getUserId()));
        wzxx.setDeptId(Math.toIntExact(getDeptId()));
        wzxx.setCreateBy(getUsername());
        return toAjax(wzxxService.insertWzxx(wzxx));
    }

    /**
     * 修改物资基本信息
     */
    @PreAuthorize("@ss.hasPermi('wzgl:wzxx:edit')")
    @Log(title = "物资基本信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Wzxx wzxx) {
        return toAjax(wzxxService.updateWzxx(wzxx));
    }

    /**
     * 删除物资基本信息
     */
    @PreAuthorize("@ss.hasPermi('wzgl:wzxx:remove')")
    @Log(title = "物资基本信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{wzxxIds}")
    public AjaxResult remove(@PathVariable Integer[] wzxxIds) {
        return toAjax(wzxxService.deleteWzxxByWzxxIds(wzxxIds));
    }

    /**
     * 查询仓库货架分层
     *
     * @return
     */
    @GetMapping("/options")
    public AjaxResult selectCkHjFc() {
        List<CkHjFcVo> list = wzxxService.selectCkHjFc(new CkHjFcVo());
        return success(list);
    }

    /**
     * 查询物资核控
     *
     * @return
     */
    @GetMapping("/selectWzHk")
    public TableDataInfo selectWzHk(WzHkVo wzHkVo) {
        startPage();
        List<WzHkVo> list = wzxxService.selectWzHk(wzHkVo);
        return getDataTable(list);
    }

    /**
     * 导出物资核控列表
     */
    @Log(title = "物资核控", businessType = BusinessType.EXPORT)
    @PostMapping("/wzhk/export")
    public void exportWzHk(HttpServletResponse response, WzHkVo wzHkVo) {
        List<WzHkVo> list = wzxxService.selectWzHk(wzHkVo);
        ExcelUtil<WzHkVo> util = new ExcelUtil<WzHkVo>(WzHkVo.class);
        util.exportExcel(response, list, "物资核控数据");
    }

    /**
     * 物资分类价值分析
     *
     */
    @GetMapping("/selectWzFlJzFx")
    public AjaxResult selectWzFlJzFx() {
        List<WzFlJzFx> list = wzxxService.selectWzFlJzFx();
        return success(list);
    }

    /**
     * 入库趋势分析
     */
    @GetMapping("/selectRkQsFx")
    public AjaxResult selectRkQsFx() {
        List<RkQsFx> list = wzxxService.selectRkQsFx();
        return success(list);
    }

    /**
     * 出库趋势分析
     */
    @GetMapping("/selectCkQsFx")
    public AjaxResult selectCkQsFx() {
        List<CkQsFx> list = wzxxService.selectCkQsFx();
        return success(list);
    }

    /**
     * 工区物资消耗分析
     */
    @GetMapping("/selectGqWzXhFx")
    public AjaxResult selectGqWzXhFx() {
        List<GqWzXhFx> list = wzxxService.selectGqWzXhFx();
        return success(list);
    }

    /**
     * 出库方式分析
     */
    @GetMapping("/selectCkFsFx")
    public AjaxResult selectCkFsFx() {
        List<CkFsFx> list = wzxxService.selectCkFsFx();
        return success(list);
    }

    /**
     * 帕累托预测分析
     */
    @GetMapping("/selectPltYcFx")
    public AjaxResult selectPltYcFx() {
        List<PltYcFx> list = wzxxService.selectPltYcFx();
        return success(list);
    }

}
